import javax.media.opengl.*;
import jocode.*;
import jomodel.*;
import processing.opengl.*;

// light position: 
//  if last value is 0, then this describes light direction.  i
//  if 1, then light position.
float lightPosition[] = { 0f, 2f, 2f, 0f };

// sphere displaylist
int sphereDL;

// holds a texture mapped font
JOFont font;

// will hold the last seven characters types
ArrayList lastchars = new ArrayList();

void setup() {
  size(800, 600, OPENGL);
  JOBridgeP5.init(this);

  // make a sphere display list
  sphereDL = JOApp.beginDisplayList(); 
  {
    JOApp.renderSphere();
  }
  JOApp.endDisplayList();

  // Make a font object to render text
  font = new JOFont(new Font("Trebuchet", Font.BOLD, 18));
}

// set the camera position, field of view, depth
void setPerspective() {
  // select projection matrix (controls perspective)
  JOApp.gl.glMatrixMode(GL.GL_PROJECTION);
  JOApp.gl.glLoadIdentity();
  // fovy, aspect ratio, zNear, zFar
  JOApp.glu.gluPerspective(40f,                // zoom in or out of view
                           JOApp.aspectRatio,  // shape of viewport rectangle
                           .1f,                // Min Z: how far from eye position does view start
                           500f);              // max Z: how far from eye position does view extend
  // return to modelview matrix
  JOApp.gl.glMatrixMode(GL.GL_MODELVIEW);
}

void draw() {
  JOApp.begin();
  {
    // clear depth buffer and color
    JOApp.gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    // setup and enable perspective
    setPerspective();

    // Create a white light
    JOApp.setLight(GL.GL_LIGHT1,
                   new float[] { 1f,  1f,  1f,  1f },  // diffuse
                   new float[] { .8f, .8f, .8f, 1f },  // ambient
                   new float[] { 1f,  1f,  1f,  1f },  // specular
                   lightPosition);

    // off-white material, very shiny
    JOApp.setMaterial(new float[] { .6f, .6f, .45f, 1 }, 1f);

    // lighten up the shadows a bit
    JOApp.setAmbientLight(new float[] { .35f, .45f, .5f, 1f });

    // select model view for subsequent transforms
    JOApp.gl.glMatrixMode(GL.GL_MODELVIEW);
    JOApp.gl.glLoadIdentity();

    // do gluLookAt() with viewpoint position, direction, orientation
    JOApp.glu.gluLookAt(0f, -3f, 33f, // where is the eye
    0f, -3f, 0f,    // what point are we looking at
    0f, 1f, 0f);   // which way is up

    // draw the scene (after we draw the shadows, so everything layers correctly)
    drawSpheres();

    // Place the light.  Light will move with the rest of the scene
    JOApp.setLightPosition(GL.GL_LIGHT1, lightPosition);

    // render some text using a font
    font.print(40, height - 80, "Type something:");

    // render some text using texture-mapped font
    JOApp.print(40, 210, "Text rendered with the default JOApp.print() function uses");
    JOApp.print(40, 190, "a texture mapped fixed width font, loaded from an image file.", 1);

    // render some text using the GLFont class
    font.print(40,125, "Text rendered with the GLFont class generates a proportional spaced");
    font.print(40,100, "character set from a Java Font object.");
  }
  JOApp.end();
}

void drawSpheres() {
  int leftpos = -12;
  int texture = 0;
  JOApp.gl.glEnable(GL.GL_LIGHTING);
  for (int i=0; i < 7; i++) {
    texture = 0;
    if (lastchars.size() > i) {
      texture = ((Integer)lastchars.get(i)).intValue();
    }
    JOApp.gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
    JOApp.gl.glPushMatrix();
    {
      JOApp.gl.glTranslatef(leftpos, 0, 0); // move
      JOApp.gl.glScalef(2f, 2f, 2f); // scale up
      JOApp.callDisplayList(sphereDL); //renderSphere();
    }
    JOApp.gl.glPopMatrix();
    leftpos += 4;
  }
}

void keyPressed() {
  if (key >= 32) {   // only printable characters
    // pick a font:
    Font font = new Font("Courier New", Font.BOLD, 120);

    // make a texture image of the character
    int textureHandle = JOFont.makeCharTexture(font, str(key), JOMaterial.colorBlack, JOMaterial.colorWhite);
    lastchars.add(textureHandle);
    if (lastchars.size() > 7) {
      int oldTxtr = ((Integer)lastchars.get(0)).intValue();
      JOApp.deleteTexture(oldTxtr);
      lastchars.remove(0);
    }
  }
}


